// packageJ/energy/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    text: "",
    active: 0,
    page: 1,
    total_page: 1,
    amounts: 0,
    contribution: "",
    list: [],
    reward_list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  click_handle(e) {
    this.setData({
      active: e.detail.index
    })
    this.getData();
  },
  onLoad() {
    this.getData();
  },
  getData(page = 1) {
    let urlStr = "plugin.contribution.frontend.controllers.center.change";
    if (this.data.active !== 0) {
      urlStr = 'plugin.contribution.frontend.controllers.center.rewards';
    }
    app._getNetWork({
      url: app.getNetAddresss(urlStr),
      data: {
        page
      },
      success: resdata => {
        let {
          data,
          result,
          msg
        } = resdata.data;
        if (result !== 1) {
          return app.tips(msg);
        }

        let {
          info,
          list
        } = data;
        wx.setNavigationBarTitle({
          title: info.name,
        })
        this.data.page = list.current_page;
        this.data.total_page = list.last_page;
        let _list = list.data;
        _list.forEach(item => {
          if (Number(item.amounts) > 0) {
            item.amount = '+' + item.amounts;
          }else{
            item.amount = item.amounts;
          }
        })
        if (this.data.active == 0) {
          let arr =(page==1)?_list:this.data.list.concat(_list);
          this.setData({list: arr})
        } else {
          let arr =(page==1)?_list:this.data.reward_list.concat(_list);
          this.setData({ reward_list: arr})
        }
        this.setData({
          text: info.name,
          amounts: info.amounts,
          contribution: info.contribution,

        })

        console.log(data, "data===")
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log(this.data.total_page, "page===", this.data.page);
    if (this.data.page >= this.data.total_page) {
      return;
    }
    let page = this.data.page + 1;
    this.getData(page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})